#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020/4/16 15:46|15:46
# @Author  : yangdingyi
# @File    : 56. 合并区间.2020-04-16
# @Software: PyCharm
from typing import List


class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        intervals.sort(key=lambda x: x[0])
        ans = [intervals[0]]
        ll = 0
        for i in range(1, len(intervals)):
            x = intervals[i]
            if x[0] <= ans[ll][1]:
                if x[1] > ans[ll][1]:
                    ans[ll][1] = x[1]
            else:
                ans.append(x)
        return ans


solve = Solution()
ii = [[1, 3], [2, 6], [8, 10], [15, 18]]
print(ii)
print(solve.merge(ii))


